package com.ulook.lookplanet.service;

import com.ulook.lookplanet.bean.TCornUser;
import com.ulook.lookplanet.bean.TUser;
import com.ulook.lookplanet.bean.VUserInfo;
import com.ulook.lookplanet.common.ComPath;
import com.ulook.lookplanet.mapper.HomeMapper;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/3/27
 */
@Service
public class HomeService {

    @Autowired
    private HomeMapper homeMapper;

    public VUserInfo getUserInfo(int id){
        return homeMapper.getUserInfo(id);
    }

    public TUser getTUserById(int id){
        return homeMapper.getTUserById(id);
    }

    /**
     * 是否关注
     */
    public TCornUser isCornUser(TCornUser cornUser){
        return homeMapper.isCornUser(cornUser);
    }

    /**
     * 点击关注
     */
    @Transactional
    public void beCornUser(TCornUser cornUser){
        if(homeMapper.isCornUser(cornUser) == null){
            homeMapper.addCornUser(cornUser);
        }else {
            homeMapper.deleteCornUser(cornUser);
        }
    }

    /**
     * 获取关注列表
     */
    public List<TUser> getFriends(int userid){
        return homeMapper.getFriends(userid);
    }

    /**
     * 修改信息
     */
    @Transactional
    public void updateUserInfo(TUser user, List<MultipartFile> files){
        TUser tUser = homeMapper.getTUserById(user.getId());
        if(!StringUtil.isEmpty(user.getName())){
            tUser.setName(user.getName());
        }
        if(user.getAge() != 0){
            tUser.setAge(user.getAge());
        }
        if(!StringUtil.isEmpty(user.getGender())){
            tUser.setGender(user.getGender());
        }
        if(!StringUtil.isEmpty(user.getStarsign())){
            tUser.setStarsign(user.getStarsign());
        }
        if(!StringUtil.isEmpty(user.getCity())){
            tUser.setCity(user.getCity());
        }
        if(!StringUtil.isEmpty(user.getHobby())){
            tUser.setHobby(user.getHobby());
        }
        if(!StringUtil.isEmpty(user.getSignature())){
            tUser.setSignature(user.getSignature());
        }

        MultipartFile image;
        if (files != null){
            if(files.size() > 0){
                image = files.get(0);
                try {
                    // 项目所在目录
                    String jar_parent  = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
                    jar_parent += File.separator+ ComPath.FILE_DIR +File.separator+ComPath.HEAD_IMG_PATH+File.separator;
                    String path = ComPath.FILE_DIR_SERVICE + File.separator + ComPath.HEAD_IMG_PATH + File.separator;
                    // 保存图片在本地
                    File image2 = new File(jar_parent + image.getOriginalFilename());
                    image.transferTo(image2);
                    // 添加文件名
                    tUser.setHeadimg(image.getOriginalFilename());
                } catch (IOException e) {
                    e.printStackTrace();
                    //return false;
                }
            }
        }

        // 保存
        homeMapper.updateUserInfo(tUser);
    }


}
